<%@ page import="com.party.core.model.system.TargetType" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../include/tag.jsp" %>
<jsp:useBean id="now" class="java.util.Date" scope="page"/>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>${member == null ? '新增' : '编辑'}重要通知</title>
    <%@include file="../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
</head>
<body>
<!--头部-->
<%@include file="../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/notify/sysMessage/save.do">
                    <input type="hidden" name="id" value="${sysMessage.id}"/>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">推送对象<span class="f-verify-red">*</span></label>
                            <div class="layui-input-block">
                                <input type="hidden" id="memberList" value="" name="memberList"/>
                                <input type="radio" name="isPushAll" value="1" lay-filter="isPushAll"
                                       lay-verify="isPushAll"
                                       title="全部对象" ${sysMessage.isPushAll == '1' ? 'checked="checked"' : ''} />
                                <input type="radio" name="isPushAll" value="0" lay-filter="isPushAll"
                                       lay-verify="isPushAll"
                                       title="单独推送" ${sysMessage.isPushAll == '0' ? 'checked="checked"' : ''} />
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item selectedMember dn">
                        <div class="layui-inline">
                            <label class="layui-form-label">&emsp;&emsp;</label>
                            <div class="layui-input-block">
                                <a href="javascript:txz.selectMemberUtil.selectMember('.selectedMember .tag-wrap');" class="layui-btn layui-btn-danger">选择合作商</a>
                                <div class="tag-wrap" style="max-width: 900px; min-width: 500px"></div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">标题<span class="f-verify-red">*</span></label>
                        <div class="layui-input-block" style="width: calc(100% - 50%); float: left; margin-left: 0; margin-right: 10px;">
                            <input type="text" name="title" lay-verify="title" maxlength="50" autocomplete="off" placeholder="标题"
                                   class="layui-input" value="${sysMessage.title}">
                        </div>
                        <div class="layui-form-mid layui-word-aux">标题最多50字符</div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">推送时间<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <input class="layui-input" type="text" name="sendTimeString" lay-verify="sendTime"
                                       value="<fmt:formatDate value="${now}" pattern="yyyy-MM-dd HH:mm:ss" />"
                                       placeholder="发送时间">
                            </div>
                            <div class="layui-form-mid layui-word-aux" style="width: 400px">发送时间小于或等于当前时间,则会立即发送</div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">内容<span class="f-verify-red">*</span></label>
                        <div class="layui-input-block">
                            <script id="ueditor1" type="text/plain"></script>
                            <div style="display: none" id="contentView">${sysMessage.content}</div>
                            <input type="hidden" name="content" id="content" lay-verify="content"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <a href="javascript:void(0)" class="layui-btn layui-btn-danger" lay-submit lay-filter="*">确定推送</a>
                            <a href="${ctx}/notify/sysMessage/webListPage.do" class="layui-btn layui-btn-primary">取消</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <!--底部-->
        <%@include file="../include/footer.jsp" %>
    </section>
    <%@include file="selectMemberInclude.jsp"%>
</div>

<script type="text/javascript" src="${ctxStatic}/UEditor/ueditor.config.js"></script>
<script type="text/javascript" src="${ctxStatic}/UEditor/ueditor.all.js"></script>
<script type="text/javascript" src="${ctxStatic}/UEditor/myplugin/uploadCI.js"></script>
<script type="text/javascript" src="${ctxStatic}/UEditor/myplugin/uploadVideo.js"></script>
<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
<script type="text/javascript" src="${ctxStatic}/UEditor/lang/zh-cn/zh-cn.js"></script>
<script>
    var ue = UE.getEditor('ueditor1', {
        initialFrameHeight: 500,//设置编辑器高度
        scaleEnabled: true
    });

    $(function () {
        layui.use(['form', 'laydate'], function () {
            var form = layui.form, laydate = layui.laydate;

            var sendTime = {
                elem: 'input[name=sendTimeString]',
                type: 'datetime'
            };
            laydate.render(sendTime);

            form.on('radio(isPushAll)', function (data) {
                if (data.value == 0) {
                    // 部分合作商
                    // txz.selectMemberUtil.openSetMember()
                    $(".selectedMember").removeClass("dn");
                } else {
                    // 全部合作商
                    $(".selectedMember").find('.tag-wrap').html('');
                    $(".selectedMember").addClass("dn");
                }
            });

            //自定义验证规则
            form.verify({
                isPushAll: function (value) {
                    var checkLength = $('[name=isPushAll]:checked').length;
                    if (checkLength == 0) {
                        return "请选择推送对象";
                    } else {
                        var checkValue = $('[name=isPushAll]:checked').val();
                        if (checkValue == 0) {
                            var _array = new Array();
                            $(".selectedMember .tag-wrap").find('.tag-item').each(function (index, ele) {
                                _array.push($(ele).data('id'));
                            })
                            if (_array.length == 0) {
                                return "请选择推送对象";
                            }
                        }
                    }
                },
                title: function (value) {
                    if (value == "") {
                        return '请输入标题';
                    }
                },
                content: function (value) {
                    $("#contentView").html(ue.getContent());
                    var content = $("#contentView").html();
                    if (!util.isValid(content)) {
                        return "请输入内容";
                    }
                },
                sendTime: function (value) {
                    if (!util.isValid(value)) {
                        return "请选择推送时间";
                    }
                }
            });
            //监听提交
            form.on('submit', function (data) {
                txz.submitObject(data.elem, function (callBack) {
                    var _array = new Array();
                    $(".selectedMember .tag-wrap").find('.tag-item').each(function (index, ele) {
                        _array.push($(ele).data('id'));
                    })
                    $("#memberList").val(_array.join(','));
                    $("#content").val(ue.getContent().replace(/&quot;/gi, ""));
                    var action = $("#myForm").attr("action");
                    $.post(action, $('#myForm').serialize(), function (res) {
                        typeof callBack === 'function' && callBack();
                        if (res.success) {
                            util.layerMsgSuccess('推送成功', function () {
                                location.href = "${ctx}/notify/sysMessage/webListPage.do";
                            });
                        } else {
                            util.layerMsgError('推送失败');
                        }
                    });
                })
                return false;
            });

        });
        ue.addListener('ready', function () {
            this.setContent($("#contentView").html());
        });

        ue.addListener('blur', function () {
            $("#contentView").html(ue.getContent());
        });
    })

    // 文本编辑器图片上传
    function uEditorUploadCI(editor) {
        txz.openSelImg({
            min: 1,
            type: '<%=TargetType.ARTICLE.getCode()%>',
            cb: function (imgs) {
                if (imgs.length) {
                    for (var i = 0, item; i < imgs.length; i++) {
                        item = imgs[i];
                        editor.focus();
                        editor.execCommand('inserthtml', item.htmlStr);
                    }
                }
            }
        })
    }

    function uEditorUploadCIVideo(editor) {
        txz.openSelVideo({
            min: 1,
            type: '<%=TargetType.ARTICLE.getCode()%>',
            cb: function (imgs) {
                if (imgs.length) {
                    for (var i = 0, item; i < imgs.length; i++) {
                        item = imgs[i];
                        editor.focus();
                        editor.execCommand('inserthtml', item.htmlStr);
                    }
                }
            }
        })
    }
</script>
</body>
<script type="text/javascript">
    txz.initHeader({
        nav: [{
            name: '重要通知管理',
            href: '${ctx}/notify/sysMessage/webListPage.do'
        }, {
            name: '${empty sysMessage.id ? '新增' : '编辑'}重要通知',
            curr: true
        }],
        btns: [{
            name: '返回',
            href: '${ctx}/notify/sysMessage/webListPage.do',
            icon: 'back'
        }]
    });
</script>
</html>